library(foreign)
library(effects)
library(nnet)
library(MASS)
library(dplyr)
library(tidyverse)
library(plyr) 
library(haven)
library(simcf)
library(ggplot2)
options(scipen=20)


## read analysis_output
tests <- read_delim(file = "tests_output.txt", delim = "\t", quote = "", col_names = TRUE)

## adjust analysis output so that county1 always has a greater value
## than county2.  This means that county1 is a treatment county.  Note
## that the chi-squared test statistic does not have sides and hence
## flipping does not affect it.
rows2flip <- which(tests$value1 < tests$value2)
for (row2flip in rows2flip) {
  ## flip county names
  temp <- tests$county1[row2flip]     
  tests$county1[row2flip] <- tests$county2[row2flip]    
  tests$county2[row2flip] <- temp
  ## flip values
  temp <- tests$value1[row2flip]     
  tests$value1[row2flip] <- tests$value2[row2flip]    
  tests$value2[row2flip] <- temp
  ## flip proportions
  temp <- tests$proportion1[row2flip]     
  tests$proportion1[row2flip] <- tests$proportion2[row2flip]    
  tests$proportion2[row2flip] <- temp
}

## add variables to test matrix; most important is hours_change_diff,
## which is the difference in some early voting measure.

## set borderankcutoff
borderrankcutoff <- 40

tests <- tests %>% mutate(prop_diff = (proportion1 - proportion2) * 100,
                          "EIP Difference" = (value1 - value2), # names w/ spaces are needed for neat legend titles
                          "Significance" = ifelse(p_value <= 0.05,"p < 0.05","p > 0.05"),
                          race = revalue(race, c("white" = "White", "black" = "Black")),
                          party = revalue(party, c("dem" = "Democrat", "rep" = "Republican")),
                          group = paste(race, party, sep = ' '))

  
# set colors
notsig_color <- "gray60"
sig_color <- "black"


# create a labeller for facets (spaces in test_type labels mean that anything we rename them in mutate will be nonsensical)
test_type <- c(
  `change_distr_novote2016_given_novote2012` = "Test: No vote 2016 given no vote 2012",
  `change_distr_vote2016_given_vote2012` = "Test: Vote 2016 given vote 2016",
  `Black Democrat` = "Black Democrat",
  `White Democrat` = "White Democrat",
  `White Republican` = "White Republican")


# DIFFERENCE IN PROPORTIONS BY DIFFERENCE IN CHANGE IN HOURS, ALL POINTS

# plot test 1 and test 2, total hours

# for this plot only, a special layer to denote the caldwell-catawba pairing detailed in the text
eg_data <- as.data.frame( tests[(tests$county1 == "CALDWELL") & (tests$county2 == "CATAWBA") &
                   (tests$test_type == "change_distr_novote2016_given_novote2012") &
  (tests$group =="White Democrat") & (tests$border_measure=="totalhoursDID"), ])

plot2save <- ggplot(filter(tests[order(tests$Significance,decreasing=T),], 
                           border_measure == "totalhoursDID",group != "Black Republican",
                           borderrank <= borderrankcutoff), 
                    aes(x = `EIP Difference`, y = prop_diff)) +
  geom_point(aes(color = `Significance`, size=`EIP Difference`),shape = 1) +
  # these next two layers denote the Catawba-Caldwell pairing referenced in text
  geom_point(data=eg_data, aes(x=`EIP Difference`, y=prop_diff),
             shape=16, size=6,color="gray50")+
  theme_bw()+theme(strip.background = element_rect(fill="white"))+
  scale_color_manual(values = c(sig_color, notsig_color)) +
  scale_y_continuous(breaks = seq(-30, 30, 10)) + 
  facet_grid(test_type ~ group, labeller = as_labeller(test_type)) +  # removed:  scales = "free_y",
  labs(y = "Percentage point difference in fraction vote/novote",
       x = "Difference in Total EIP hour changes\n")

ggsave (plot2save, filename = paste("diffprop_diffTOTALhours", ".pdf", sep = ""),
        width = 7, height = 7)


# plot test 1 and test 2, evening hours

plot2save <- ggplot(filter(tests[order(tests$Significance,decreasing=T),], 
                           border_measure == "evehoursDID",group != "Black Republican",
                           borderrank <= borderrankcutoff), 
                    aes(x = `EIP Difference`, y = prop_diff)) +
  geom_point(aes(color = Significance,size=`EIP Difference`), shape=1) +
  theme_bw()+theme(strip.background = element_rect(fill="white"))+
  scale_color_manual(values = c(sig_color, notsig_color)) +
  scale_y_continuous(breaks = seq(-30, 30, 10)) + 
  facet_grid(test_type ~ group, labeller = as_labeller(test_type)) +  # removed: scales = "free_y"
  labs(y = "Percentage point difference in fraction vote/novote",
       x = "Difference in evening EIP hour changes\n")

ggsave (plot2save, filename = paste("diffprop_diffEVENINGhours", ".pdf", sep = ""),
        width = 7, height = 7)

# plot test 1 and test 2, saturday hours

plot2save <- ggplot(filter(tests[order(tests$Significance,decreasing=T),], 
                           border_measure == "sathoursDID", group != "Black Republican",
                           borderrank <= borderrankcutoff), 
                    aes(x = `EIP Difference`, y = prop_diff)) +
  geom_point(aes(color = Significance,size=`EIP Difference`),shape=1) +
  theme_bw()+theme(strip.background = element_rect(fill="white"))+
  scale_color_manual(values = c(sig_color, notsig_color)) +
  scale_y_continuous(breaks = seq(-30, 30, 10)) + 
  facet_grid(test_type ~ group, labeller = as_labeller(test_type)) +  
  labs(y = "Percentage point difference in fraction vote/novote",
       x = "Difference in Saturday EIP hour changes\n")

ggsave (plot2save, filename = paste("diffprop_diffSATURDAYhours", ".pdf", sep = ""),
        width = 7, height = 7)

# plot test 1 and test 2, sunday hours

plot2save <- ggplot(filter(tests[order(tests$Significance,decreasing=T),],
                           border_measure == "sunhoursDID", group != "Black Republican",
                           borderrank <= borderrankcutoff), 
                    aes(x = `EIP Difference`, y = prop_diff)) +
  geom_point(aes(color = Significance,size=`EIP Difference`), shape=1) +
  theme_bw()+theme(strip.background = element_rect(fill="white"))+
  scale_color_manual(values = c(sig_color, notsig_color)) +
  scale_y_continuous(breaks = seq(-30, 30, 10)) + 
  facet_grid(test_type ~ group, labeller = as_labeller(test_type)) + # removed: scales = "free_y", 
  labs(y = "Percentage point difference in fraction vote/novote",
       x = "Difference in Sunday EIP hour changes\n")

ggsave (plot2save, filename = paste("diffprop_diffSUNDAYhours", ".pdf", sep = ""),
        width = 7, height = 7)

# plot test 1 and test 2, total sites


plot2save <- ggplot(filter(tests[order(tests$Significance,decreasing=T),], 
                           border_measure == "sitesDID", group != "Black Republican",
                           borderrank <= borderrankcutoff), 
                    aes(x = `EIP Difference`, y = prop_diff)) +
  geom_point(aes(color = Significance, size=`EIP Difference`), shape=1) +
  theme_bw()+theme(strip.background = element_rect(fill="white"))+
  scale_color_manual(values = c(sig_color, notsig_color)) +
  scale_y_continuous(breaks = seq(-30, 30, 10)) + 
  facet_grid(test_type ~ group, labeller = as_labeller(test_type)) +  # removed:  scales = "free_y",
  labs(y = "Percentage point difference in fraction vote/novote",
       x = "Difference in EIP site location changes\n")

ggsave (plot2save, filename = paste("diffprop_diffSITES", ".pdf", sep = ""),
        width = 7, height = 7)


